package com.project.pet.service;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.pet.common.ResponseData;
import com.project.pet.mapper.InvitationMapper;
import com.project.pet.pojo.Invitation;
import com.project.pet.utils.ResponseDataUtil;
import com.project.pet.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvitationService {
    @Autowired
    private InvitationMapper mapper;

    @Autowired
    private HttpServletRequest request;

    public ResponseData<Object> insert(Invitation invitation){
        invitation.setSender(Integer.parseInt(request.getAttribute("wxUserId").toString()));
        Example example = new Example(Invitation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sender",invitation.getSender());
        criteria.andEqualTo("receiver",invitation.getReceiver());
        List<Invitation> invitations = mapper.selectByExample(example);
        if(!invitations.isEmpty()){
            String time  = invitations.get(0).getTime();
            Invitation in = invitations.get(0);
            if("1".equals(in.getStatus()) && "1".equals(invitation.getSenderDelete())){
                return ResponseDataUtil.buildError("您曾经向她/他发送过邀约申请了,记录已恢复，请查看消息");
            }
            if("1".equals(in.getStatus()) && "0".equals(invitation.getSenderDelete())){
                return ResponseDataUtil.buildError("您曾经向她/他发送过邀约申请了,请查看消息记录");
            }
            boolean sameDay = DateUtil.isSameDay(DateUtil.parse(time), new Date());
            if(!sameDay){
                in.setTime(DateUtil.now());
                mapper.updateByPrimaryKey(in);
                return ResponseDataUtil.buildSuccess("发送成功，请耐心等待回复哦");
            }else{
                return ResponseDataUtil.buildError("您今天已经向她/他发送过邀约申请了");
            }
        }else{
            invitation.setReceiverDelete("0");
            invitation.setSenderDelete("0");
            invitation.setStatus("-1");
            invitation.setTime(DateUtil.now());
            mapper.insertSelective(invitation);
            return ResponseDataUtil.buildSuccess("发送成功，请耐心等待回复哦");
        }
    }
    public ResponseData<Object> updateByPrimaryKey(Invitation invitation){
        int res = mapper.updateByPrimaryKeySelective(invitation);
        return res>0?ResponseDataUtil.buildSuccess("操作成功"):ResponseDataUtil.buildError("操作失败");
    }
    public ResponseData<Object> pagingQuery(HashMap<String,Object> map){
        Integer pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        map.remove("pageIndex");
        map.remove("pageSize");
        Example example = new Example(Invitation.class);
        Example.Criteria criteria = example.createCriteria();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
            if(!StringUtils.isEmpty(value))
                criteria.andLike(key,"%"+value+"%");
        }
        PageHelper.startPage(pageIndex,pageSize);
        List<Invitation> list = mapper.selectByExample(example);
        PageInfo<Invitation> pageInfo = new PageInfo<>(list);
        return ResponseDataUtil.buildSuccess(pageInfo);
    }
    @Transactional
    public ResponseData<Object> delete(List<Integer> ids){
        try{
            for (Integer id : ids) {
                mapper.deleteByPrimaryKey(id);
            }
            return ResponseDataUtil.buildSuccess("操作成功");
        }catch (Exception e){
            return ResponseDataUtil.buildError("操作失败");
        }
    }
    public ResponseData<Object> selectAll(){
        return ResponseDataUtil.buildSuccess(mapper.selectAll());
    }
    public ResponseData<Object> selectByMap(HashMap<String,Object> map){
        Example example = new Example(Invitation.class);
        Example.Criteria criteria = example.createCriteria();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
            if(!StringUtils.isEmpty(value))
                criteria.andEqualTo(key,value);
        }
        return ResponseDataUtil.buildSuccess(mapper.selectByExample(example));
    }
}
